package ahblue.cn.tanzi.data.source;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;

import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import ahblue.cn.tanzi.TanZiApplication;
import ahblue.cn.tanzi.common.AppException;
import ahblue.cn.tanzi.common.Config;
import ahblue.cn.tanzi.data.HomeArticle;
import ahblue.cn.tanzi.data.Result;
import ahblue.cn.tanzi.model.SearchWord;
import ahblue.cn.tanzi.model.VersionInfo;
import ahblue.cn.tanzi.model.circle.Circle;
import ahblue.cn.tanzi.model.circle.CircleArticleItem;
import ahblue.cn.tanzi.model.circle.CircleCategory;
import ahblue.cn.tanzi.model.circle.CircleDetailCenter;
import ahblue.cn.tanzi.model.circle.CircleDetailTop;
import ahblue.cn.tanzi.model.circle.CircleDynamicItem;
import ahblue.cn.tanzi.model.circle.DynamicComment;
import ahblue.cn.tanzi.model.circle.DynamicFabulou;
import ahblue.cn.tanzi.model.circle.ShotDynamicDetail;
import ahblue.cn.tanzi.model.collect.CollectModel;
import ahblue.cn.tanzi.model.home.ArticleCategory;
import ahblue.cn.tanzi.model.home.ArticleDetail;
import ahblue.cn.tanzi.model.home.ArticlesForCategory;
import ahblue.cn.tanzi.model.home.BannerAd;
import ahblue.cn.tanzi.model.home.HomeModel;
import ahblue.cn.tanzi.model.home.NewsForCategory;
import ahblue.cn.tanzi.model.home.SpecalModel;
import ahblue.cn.tanzi.model.home.VideoCommentlModel;
import ahblue.cn.tanzi.model.home.VideoDetailModel;
import ahblue.cn.tanzi.model.home.VideoLableModel;
import ahblue.cn.tanzi.model.home.VideoModel;
import ahblue.cn.tanzi.model.user.BankListModel;
import ahblue.cn.tanzi.model.user.CodeModel;
import ahblue.cn.tanzi.model.user.CostModel;
import ahblue.cn.tanzi.model.user.KeChengModel;
import ahblue.cn.tanzi.model.user.LoginResultModel;
import ahblue.cn.tanzi.model.user.LookHisToryModel;
import ahblue.cn.tanzi.model.user.MemberInfo;
import ahblue.cn.tanzi.model.user.PackageModel;
import ahblue.cn.tanzi.model.user.ProfitModel;
import ahblue.cn.tanzi.model.user.TeamModel;
import ahblue.cn.tanzi.model.user.TieziModel;
import ahblue.cn.tanzi.model.user.UserModel;
import ahblue.cn.tanzi.ui.msg.model.FriendModel;
import ahblue.cn.tanzi.ui.person.pay.WAPayNode;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import okio.Buffer;
import okio.BufferedSource;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;


/**
 * 远程数据源
 *
 * Created by Jack Shao (jack.shao@qq.com) 2018-11-08
 */
public final class RemoteRepository {

    private static int SUCCESS_CODE = 200;
    private static int TOKEN_ERROR = 80003;
    private static int OTHER_DEVICE_LOGINED = 8000201;

    private static final RemoteRepository sInstance = new RemoteRepository();
    private final ApiService mApiService;

    private RemoteRepository() {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .connectTimeout(60, TimeUnit.SECONDS)
                .addInterceptor(interceptor)
                .addInterceptor(new MyInterceptor()).build();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Config.baseUrl)
                .client(client)
                .addConverterFactory(ScalarsConverterFactory.create())//增加返回值为String的支持
                .addConverterFactory(NobodyConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        mApiService = retrofit.create(ApiService.class);
    }

    public static RemoteRepository getsInstance() {
        return sInstance;
    }

    private class MyInterceptor implements Interceptor {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            Response response = chain.proceed(request);
            ResponseBody responseBody = response.body();
            long contentLength = responseBody.contentLength();
            if (!bodyEncoded(response.headers())) {
                BufferedSource source = responseBody.source();
                source.request(Long.MAX_VALUE);
                Buffer buffer = source.buffer();
                Charset charset = UTF8;
                MediaType contentType = responseBody.contentType();
                if (contentType != null) {
                    try {
                        charset = contentType.charset(UTF8);
                    } catch (UnsupportedCharsetException e) {
                        return response;
                    }
                }
                if (!isPlaintext(buffer)) {
                    return response;
                }
                if (contentLength != 0 && charset != null) {
                    String result = buffer.clone().readString(charset);
                    int code = JSONObject.parseObject(result).getInteger("code");
                    if (code == OTHER_DEVICE_LOGINED) {
                        Intent intent = new Intent();
                        intent.setAction(MyHttpReceiver.ACTION_OTHER_DEVICE_LOGINED);
                        TanZiApplication.getContext().sendBroadcast(intent);
                    }
                }
            }
            return response;
        }
    }

    private static final Charset UTF8 = Charset.forName("UTF-8");

    private boolean bodyEncoded(Headers headers) {
        String contentEncoding = headers.get("Content-Encoding");
        return contentEncoding != null && !contentEncoding.equalsIgnoreCase("identity");
    }

    static boolean isPlaintext(Buffer buffer) throws EOFException {
        try {
            Buffer prefix = new Buffer();
            long byteCount = buffer.size() < 64 ? buffer.size() : 64;
            buffer.copyTo(prefix, 0, byteCount);
            for (int i = 0; i < 16; i++) {
                if (prefix.exhausted()) {
                    break;
                }
                int codePoint = prefix.readUtf8CodePoint();
                if (Character.isISOControl(codePoint) && !Character.isWhitespace(codePoint)) {
                    return false;
                }
            }
            return true;
        } catch (EOFException e) {
            return false; // Truncated UTF-8 sequence.
        }
    }

    /*----------------------------------------------------------------------------------------------
     * 与首页相关的接口
     *--------------------------------------------------------------------------------------------*/

    /**
     * 获取首页顶部的广告条。
     * @param callback 回调接口
     */
    public Disposable getBigShotIndexTop(final ApiCallback<List<BannerAd>> callback) {
        return mApiService.getBigShotIndexTop()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<BannerAd>>>() {
                               @Override
                               public void accept(Result<List<BannerAd>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 获取首页的列表数据。
     *
     * @param page 页码
     * @param pageCount 一页数量（返回是输入的两倍）
     * @param callback 回调接口
     */
    public Disposable getBigShotIndexList(int page, int pageCount, final ApiCallback<List<HomeArticle>> callback) {
        return mApiService.getBigShotIndexList(page, pageCount)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<HomeArticle>>>() {
                               @Override
                               public void accept(Result<List<HomeArticle>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 获取首页的一级菜单
     *
     * @param callback 回调接口
     */
    public Disposable getBigShotIndexCategory(final ApiCallback<List<HomeModel>> callback) {
        return mApiService.getBigShotArticleCategory()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<HomeModel>>>() {
                               @Override
                               public void accept(Result<List<HomeModel>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 获取文章菜单
     * @param callback 回调接口
     */
    public Disposable getArticleCategory(int typeid, final ApiCallback<List<ArticleCategory>> callback) {
        return mApiService.getCategoryList(typeid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<ArticleCategory>>>() {
                               @Override
                               public void accept(Result<List<ArticleCategory>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 圈子详情-圈子文章
     * @param callback 圈子详情-圈子文章
     */
    public Disposable getCircleDetailsArticle(int circleId, int page, int pageCount,final ApiCallback<List<CircleArticleItem>> callback) {
        return mApiService.getCircleDetailsArticle(circleId, page, pageCount)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<CircleArticleItem>>>() {
                               @Override
                               public void accept(Result<List<CircleArticleItem>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 获取对应文章列表(及专题列表)。
     * @param categoryId 文章类目id（获取专题时不用传）
     * @param page 页码
     * @param pageCount 一页数量
     * @param isSpecial 是否专题（获取专题文章时才传1）不是传0
     * @param name 模糊搜索词
     * @param callback 回调接口
     */
    public Disposable getBigShotArticleList(int categoryId, int page, int pageCount, int isSpecial, String name,
                                            final ApiCallback<List<ArticlesForCategory>> callback) {
        return mApiService.getBigShotArticleList(categoryId, page, pageCount, isSpecial, name)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<ArticlesForCategory>>>() {
                               @Override
                               public void accept(Result<List<ArticlesForCategory>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 圈子详情里面获取圈子介绍
     * @param  id 圈子id
     * @param callback 回调接口
     */
    public Disposable getBigShotCircleIntroduce(int  id,
                                            final ApiCallback<String> callback) {
        return mApiService.getBigShotCircleIntroduce(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<String>>() {
                               @Override
                               public void accept(Result<String> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 3、文章详情
     * @param articleId 文章id
     * @return 文章详情
     */
    public Disposable getBigShotArticleDetail(int articleId, String token,
                                        final ApiCallback<ArticleDetail> callback) {
        return mApiService.getBigShotArticleDetail(articleId, token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<ArticleDetail>>() {
                               @Override
                               public void accept(Result<ArticleDetail> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 文章点赞和收藏。
     * @param articleId 文章id
     * @param operation 收藏或者点赞, 收藏collection，点赞fabulous
     * @param token 登录接口返回的token字段
     * @param callback 回调接口
     */
    public Disposable getBigShotArticleCollection(int articleId, String operation, String token,
                                            final ApiCallback<Result> callback) {
        return mApiService.getBigShotArticleCollection(articleId, operation, token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result>() {
                               @Override
                               public void accept(Result result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result);
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 5、评论通用接口
     * @param articleId 文章(动态)id
     * @param commentType 评论类型, comment_type=1（评论文章）comment_type=2（评论课程）
     *                    comment_type=3（评论视频）comment_type=5（动态评论）
     * @param content 评论内容
     * @param token 登录接口返回的token字段
     * @return 操作状态
     */
    public Disposable addBigShotArticleComment(int articleId, int commentType, String content, String token,
                                         ApiCallback<Result> callback) {
        return mApiService.addBigShotArticleComment(articleId, commentType, content, token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result>() {
                               @Override
                               public void accept(Result result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result);
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }



    /**
     * 7、文章取消点赞
     * @param articleId 文章(动态)id
     * @param token 登录接口返回的token字段
     * @return 操作状态
     */
    public Disposable delBigShotArticleDeleteFabulous(int articleId, String token, ApiCallback<Result> callback) {
        return mApiService.delBigShotArticleDeleteFabulous(articleId, token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result>() {
                               @Override
                               public void accept(Result result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result);
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 热门搜索.
     *
     * @param callback 回调接口
     */
    public Disposable getBigShotHotWord(final ApiCallback<List<SearchWord>> callback) {
        return mApiService.getBigShotHotWord()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<SearchWord>>>() {
                               @Override
                               public void accept(Result<List<SearchWord>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 3、添加搜索我的历史标签
     * @param token 登录接口返回的token字段
     * @param name 搜索词
     */
    public Disposable addBigShotAddWord(String token, String name, ApiCallback<Result> callback) {
        return mApiService.addBigShotAddWord(token, name)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result>() {
                               @Override
                               public void accept(Result result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   if (callback != null) {
                                       callback.onSuccess(result);
                                   }
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                if (callback != null) {
                                    callback.onFailed(errorMsg);
                                }
                            }
                        });
    }

    /**
     * 4、历史搜索词列表
     * @param token 登录接口返回的token字段
     * @param callback 回调接口
     */
    public Disposable getBigShotHistoryList(String token, final ApiCallback<List<SearchWord>> callback) {
        return mApiService.getBigShotHistoryList(token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<SearchWord>>>() {
                               @Override
                               public void accept(Result<List<SearchWord>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 5、清楚我的搜索历史
     * @param token 登录接口返回的token字段
     * @return 操作状态
     */
    public Disposable getBigShotHistoryDelete(String token, final ApiCallback<Result> callback) {
        return mApiService.getBigShotHistoryDelete(token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result>() {
                               @Override
                               public void accept(Result result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result);
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 获取专题数据
     * @param callback
     */
    public Disposable getSpecalMsg(int page,int pageCount, final ApiCallback<List<SpecalModel>> callback) {
        return mApiService.getSpecalList(page+"",pageCount+"")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<SpecalModel>>>() {
                               @Override
                               public void accept(Result<List<SpecalModel>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/





    /*----------------------------------------------------------------------------------------------
     * 与圈子相关的接口
     *--------------------------------------------------------------------------------------------*/
    public Disposable getCircleTop(final ApiCallback<List<Circle>> callback) {
        return mApiService.getCircleTop()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<Circle>>>() {
                               @Override
                               public void accept(Result<List<Circle>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 获取圈子首页动态广场数据。
     *
     * @param page 页码
     * @param pageCount 一页数量
     * @param callback 回调接口
     */
    public Disposable getBigShotUserDynamic(int page, int pageCount, String name, String token,
                                      final ApiCallback<List<CircleDynamicItem>> callback) {
        return mApiService.getBigShotUserDynamic(page, pageCount, name, token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<CircleDynamicItem>>>() {
                               @Override
                               public void accept(Result<List<CircleDynamicItem>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 获取动态详情。
     * @param shotId 动态的id
     * @param callback 回调接口
     */
    @SuppressLint("CheckResult")
    public Disposable getShotDynamicDetails(int shotId, final ApiCallback<ShotDynamicDetail> callback) {
        return mApiService.getShotDynamicDetails(shotId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<ShotDynamicDetail>>() {
                               @Override
                               public void accept(Result<ShotDynamicDetail> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 4、动态点赞列表
     * @param page 页码
     * @param pageCount 一页数量
     * @param shotId 动态id
     * @param callback 回调接口
     */
    public Disposable getBigShotDynamicFabulous(int page, int pageCount, int shotId,
                                          ApiCallback<List<DynamicFabulou>> callback) {
        return mApiService.getBigShotDynamicFabulous(page, pageCount, shotId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<DynamicFabulou>>>() {
                               @Override
                               public void accept(Result<List<DynamicFabulou>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 5、动态评论列表
     * @param page 页码
     * @param pageCount 一页数量
     * @param shotId 动态id
     * @param callback 回调接口
     */
    public Disposable getBigShotDynamicCommentList(int page, int pageCount, int shotId, ApiCallback<List<DynamicComment>> callback) {
        return mApiService.getBigShotDynamicCommentList(page, pageCount, shotId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<DynamicComment>>>() {
                               @Override
                               public void accept(Result<List<DynamicComment>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 我的圈子-三个推荐圈子。
     * @param page 页码
     * @param pageCount 一页数量
     * @param callback 回调接口
     */
    public Disposable getBigShotCircleThreeCircle(int page, int pageCount, final ApiCallback<List<Circle>> callback) {
        return mApiService.getBigShotCircleThreeCircle(page, pageCount)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<Circle>>>() {
                               @Override
                               public void accept(Result<List<Circle>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 圈子详情-顶部
     * @param circleId 圈子id
     * @param callback 回调接口
     */
    public Disposable getBigShotCircleDetailsTop(int circleId, ApiCallback<CircleDetailTop> callback) {
        return mApiService.getBigShotCircleDetailsTop(circleId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<CircleDetailTop>>() {
                               @Override
                               public void accept(Result<CircleDetailTop> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 圈子详情-中间
     * @param circleId 圈子id
     * @param callback 回调接口
     */
    public Disposable getBigShotCircleDetailsCenter(int circleId, ApiCallback<CircleDetailCenter> callback) {
        return mApiService.getBigShotCircleDetailsCenter(circleId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<CircleDetailCenter>>() {
                               @Override
                               public void accept(Result<CircleDetailCenter> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 6、圈子标签获取
     * @return 圈子标签
     */
    public Disposable getBigShotCircleCategory(ApiCallback<List<CircleCategory>> callback) {
        return mApiService.getBigShotCircleCategory()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<CircleCategory>>>() {
                               @Override
                               public void accept(Result<List<CircleCategory>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 7、对应圈子列表
     *
     * @param page 页码
     * @param pageCount 一页数量
     * @param categoryId 标签id
     * @param name 模糊搜索词
     *
     * @return 圈子列表
     */
    public Disposable getBigShotCategoryCircle(int page, int pageCount, int categoryId, String name,
                                         ApiCallback<List<Circle>> callback) {
        Observable<Result<List<Circle>>> observable;
        if (categoryId == -1) {
            observable = mApiService.getAllBigShotCategoryCircle(page, pageCount, name);
        } else {
            observable = mApiService.getBigShotCategoryCircle(page, pageCount, categoryId, name);
        }
        return observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<Circle>>>() {
                               @Override
                               public void accept(Result<List<Circle>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 11、圈子详情-圈子动态
     *
     * @param circleId 圈子id
     * @param page 页码
     * @param pageCount 一页数量
     *
     * @return 圈子动态列表
     */
    public Disposable getBigShotCircleDetailsDynamic(int circleId, int page, int pageCount,
                                                     ApiCallback<List<CircleDynamicItem>> callback) {
        return mApiService.getBigShotCircleDetailsDynamic(circleId, page, pageCount)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<CircleDynamicItem>>>() {
                               @Override
                               public void accept(Result<List<CircleDynamicItem>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 14、我的圈子-加入圈子
     * @param token 用戶登陸后獲取到的的token
     * @param circleId 圈子id
     *
     * @return 操作后的狀態，成功或者失敗
     */
    public Disposable getBigShotCommentInfoCircle(String token, int circleId, ApiCallback<Result> callback) {
        return mApiService.getBigShotCommentInfoCircle(token, circleId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result>() {
                               @Override
                               public void accept(Result result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result);
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 15、我的圈子列表
     * @param token 用户token
     * @param callback 回调接口
     */
    public Disposable getBigShotCommentMyCircle(String token, ApiCallback<List<Circle>> callback) {
        return mApiService.getBigShotCommentMyCircle(token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<Circle>>>() {
                               @Override
                               public void accept(Result<List<Circle>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 16、动态发布
     * @param token 用户登录后获取的token
     * @param content 内容, 必传
     * @param circleIds 圈子id, 例如,12,3,4,5,(前后两个,必须有)（必传）
     * @param isFriend 是否朋友可见, 0不是1是（必传）
     * @param imagePaths 文件name, 多文件file[]
     * @param callback 回调接口
     */
    public Disposable addBigShotCircleSendDynamic(String token, String content, String circleIds, int isFriend,
                                            List<String> imagePaths, String videoFileName, ApiCallback<Result> callback) {

        RequestBody tokenBody       = RequestBody.create(MediaType.parse("text/plain"), token);
        RequestBody contentBody     = RequestBody.create(MediaType.parse("text/plain"), content);
        RequestBody circleIdsBody   = RequestBody.create(MediaType.parse("text/plain"), circleIds);
        RequestBody isFriendBody    = RequestBody.create(MediaType.parse("text/plain"), Integer.toString(isFriend));

        HashMap<String, RequestBody> maps = new HashMap<>();
        if (imagePaths != null && imagePaths.size() > 0) {
            for (int i = 0; i < imagePaths.size(); i++) {
                File file = new File(imagePaths.get(i));
                RequestBody fileBody = RequestBody.create(MediaType.parse(imagePaths.get(i)), file);
                maps.put(String.format("file_name[%d]\"; filename=\"%s", i, file.getName()), fileBody);
            }
        }

        MultipartBody.Part videoBody = null;
        if (videoFileName != null && videoFileName.length() > 0) {
            File file = new File(videoFileName);
            RequestBody fileBody = RequestBody.create(MediaType.parse(videoFileName), file);
            // MultipartBody.Part is used to send also the actual file name
            MultipartBody.Part body = MultipartBody.Part.createFormData("file_movie", file.getName(), fileBody);
        }

        return mApiService.addBigShotCircleSendDynamic(tokenBody, contentBody, circleIdsBody, isFriendBody, maps, videoBody)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result>() {
                               @Override
                               public void accept(Result result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result);
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 17、圈子排行
     * @return 圈子列表
     */
    public Disposable getBigShotCircleCircleSort(ApiCallback<List<Circle>> callback) {
        return mApiService.getBigShotCircleCircleSort()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<Circle>>>() {
                               @Override
                               public void accept(Result<List<Circle>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 18、动态取消点赞
     * @param id 动态id
     * @param token 登录接口返回的token字段
     * @param callback 回调接口
     */
    public Disposable deleteBigShotArticleDeleteDynamicFabulous(int id, String token, ApiCallback<Result> callback) {
        return mApiService.deleteBigShotArticleDeleteDynamicFabulous(id, token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result>() {
                               @Override
                               public void accept(Result result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result);
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 19、动态点赞
     * @param id 动态id
     * @param token 登录接口返回的token字段
     * @param callback 回调接口
     */
    public Disposable addBigShotDynamicToFabulous(int id, String token, ApiCallback<Result> callback) {
        return mApiService.addBigShotDynamicToFabulous(id, token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result>() {
                               @Override
                               public void accept(Result result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result);
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }



    /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/



    /**
     * 个人详情接口（系统中点击其他的用户头像进入）
     * @param token 用户登录后获取的token
     * @param memberId 用户id
     * @return
     */
    public Disposable getMemberInfo(String token, long memberId, ApiCallback<MemberInfo> callback) {
        return mApiService.getMemberInfo(token, memberId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<MemberInfo>>() {
                               @Override
                               public void accept(Result<MemberInfo> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }




   /**
     * 密码登陆
     * @param name
     * @param pwd
     * @param callback
     */
    public Disposable LoginWithPwd(String name,String pwd,final ApiCallback<LoginResultModel> callback) {

        final Map params =new HashMap<>();
        params.put("member_name",name);
        params.put("pass_word",pwd);

        return mApiService.LoginWithPwd(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<LoginResultModel>>() {
                               @Override
                               public void accept(Result<LoginResultModel> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    public Disposable WxLogin(String code,final ApiCallback<LoginResultModel> callback) {

        final Map params =new HashMap<>();
        params.put("code",code);

        return mApiService.WXLogin(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<LoginResultModel>>() {
                               @Override
                               public void accept(Result<LoginResultModel> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }


    public Disposable setCode(String code,String token,final ApiCallback<LoginResultModel> callback) {

        final Map params =new HashMap<>();
        params.put("invite_code",code);
        params.put("token",token);

        return mApiService.setCode(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<LoginResultModel>>() {
                               @Override
                               public void accept(Result<LoginResultModel> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE && result.getCode() != 80002) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 验证码登录
     * @param name
     * @param pwd
     * @param callback
     */
    public Disposable LoginWithCode(String name,String pwd,final ApiCallback<LoginResultModel> callback) {

        final Map params =new HashMap<>();
        params.put("member_mobile",name);
        params.put("code",pwd);

        return mApiService.LoginWithCode(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<LoginResultModel>>() {
                               @Override
                               public void accept(Result<LoginResultModel> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }


    public Disposable update(String name,String pwd,final ApiCallback<LoginResultModel> callback) {

        final Map params =new HashMap<>();
        params.put("member_mobile",name);
        params.put("code",pwd);
        params.put("token",LoginResultModel.getToken(TanZiApplication.getContext()));

        return mApiService.update(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<LoginResultModel>>() {
                               @Override
                               public void accept(Result<LoginResultModel> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 账号注册
     * @param name
     * @param pwd
     * @param callback
     */
    public Disposable RegistWithCode(String name,String pwd,String invite_code,String code,final ApiCallback<LoginResultModel> callback) {

        final Map params =new HashMap<>();
        params.put("member_mobile",name);
        params.put("invite_code",invite_code);
        params.put("code",code);
        params.put("pass_word",pwd);

        return mApiService.RegistWithCode(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<LoginResultModel>>() {
                               @Override
                               public void accept(Result<LoginResultModel> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 验证码获取
     * @param phone
     * @param callback
     */
    public Disposable getLoginCode(String phone,final ApiCallback<CodeModel> callback) {

        final Map params =new HashMap<>();
        params.put("member_mobile",phone);
        return mApiService.getLoginCode(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<CodeModel>>() {
                               @Override
                               public void accept(Result<CodeModel> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    public Disposable updatePhone(String phone,final ApiCallback<CodeModel> callback) {

        final Map params =new HashMap<>();
        params.put("member_mobile",phone);
        params.put("token",LoginResultModel.getToken(TanZiApplication.getContext()));
        return mApiService.updatePone(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<CodeModel>>() {
                               @Override
                               public void accept(Result<CodeModel> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 获取注册验证码
     * @param phone
     * @param invite_code
     * @param callback
     */
    public Disposable getRegistCode(String phone,String invite_code,final ApiCallback<CodeModel> callback) {
        final Map params =new HashMap<>();
        params.put("member_mobile",phone);
        params.put("invite_code",invite_code);
        return mApiService.getRegistCode(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<CodeModel>>() {
                               @Override
                               public void accept(Result<CodeModel> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }


    public Disposable getMoney(String recharge_amount,String token,final ApiCallback<String> callback) {
        final Map params =new HashMap<>();
        params.put("amount",recharge_amount);
        params.put("token",token);
        return mApiService.getMoney(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<String>>() {
                               @Override
                               public void accept(Result<String> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }


    public Disposable getMoneyList(String token,final ApiCallback<List<BankListModel>> callback) {
        final Map params =new HashMap<>();
        params.put("token",token);
        return mApiService.getMoneyList(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<BankListModel>>>() {
                               @Override
                               public void accept(Result<List<BankListModel>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }


    public Disposable getProfitList(String token,final ApiCallback<List<ProfitModel>> callback) {
        final Map params =new HashMap<>();
        params.put("token",token);
        return mApiService.getProfitList(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<ProfitModel>>>() {
                               @Override
                               public void accept(Result<List<ProfitModel>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }


    public Disposable getCost(String token,final ApiCallback<List<CostModel>> callback) {
        final Map params =new HashMap<>();
        params.put("token",token);
        return mApiService.getCost(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<CostModel>>>() {
                               @Override
                               public void accept(Result<List<CostModel>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }


    public Disposable getTiezi(String token,final ApiCallback<List<TieziModel>> callback) {
        final Map params =new HashMap<>();
        params.put("token",token);
        return mApiService.getTieZi(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<TieziModel>>>() {
                               @Override
                               public void accept(Result<List<TieziModel>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }


    public Disposable getKeCheng(String token,final ApiCallback<List<KeChengModel>> callback) {
//        final Map params =new HashMap<>();
//        params.put("token",token);
        return mApiService.getKecheng(token,0,100)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<KeChengModel>>>() {
                               @Override
                               public void accept(Result<List<KeChengModel>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }


    public Disposable getMoneyDetail(String token,final ApiCallback<PackageModel> callback) {
        final Map params =new HashMap<>();
        params.put("token",token);
        return mApiService.getMoneyDetail(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<PackageModel>>() {
                               @Override
                               public void accept(Result<PackageModel> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }


    public Disposable getOrderPayNo(String recharge_amount,String token,final ApiCallback<CodeModel> callback) {
        final Map params =new HashMap<>();
        params.put("recharge_amount",recharge_amount);
        params.put("token",token);
        return mApiService.getOrderPayNo(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<CodeModel>>() {
                               @Override
                               public void accept(Result<CodeModel> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }


    public Disposable getVipOrderPayNo(String token,String viptype,final ApiCallback<CodeModel> callback) {
        final Map params =new HashMap<>();
        params.put("vip_type",viptype);
        params.put("token",token);
        return mApiService.getVipOrderPayNo(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<CodeModel>>() {
                               @Override
                               public void accept(Result<CodeModel> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }


    public Disposable pay(String token, String order_sn, String payment_code,final ApiCallback<WAPayNode> callback) {
        final Map params =new HashMap<>();
        params.put("token",token);
        params.put("order_sn",order_sn);
        params.put("payment_code",payment_code);
        return mApiService.pay(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<WAPayNode>>() {
                               @Override
                               public void accept(Result<WAPayNode> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }


    public Disposable payVip(String token, String order_sn, String payment_code,final ApiCallback<WAPayNode> callback) {
        final Map params =new HashMap<>();
        params.put("token",token);
        params.put("order_sn",order_sn);
        params.put("payment_code",payment_code);
        return mApiService.payVip(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<WAPayNode>>() {
                               @Override
                               public void accept(Result<WAPayNode> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }


    public Disposable inviteRealCode(String member_name,
                               String bank_card,
                               String banalipay_account,
                               String identity_card,
                               String token,final ApiCallback<CodeModel> callback) {
        final Map params =new HashMap<>();
        params.put("member_name",member_name);
        params.put("bank_card",bank_card);
        params.put("alipay_account",banalipay_account);
        params.put("identity_card",identity_card);
        params.put("token",token);
        return mApiService.realSelf(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<CodeModel>>() {
                               @Override
                               public void accept(Result<CodeModel> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }


    public Disposable setProfit(String profile,String token,final ApiCallback<CodeModel> callback) {
        final Map params =new HashMap<>();
        params.put("profile",profile);
        params.put("token",token);
        return mApiService.setProfit(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<CodeModel>>() {
                               @Override
                               public void accept(Result<CodeModel> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }


    public Disposable setNickName(String profile,String token,final ApiCallback<CodeModel> callback) {
        final Map params =new HashMap<>();
        params.put("member_name",profile);
        params.put("token",token);
        return mApiService.setNickName(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<CodeModel>>() {
                               @Override
                               public void accept(Result<CodeModel> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }


    public Disposable loginOut(String token,final ApiCallback<String> callback) {
        final Map params =new HashMap<>();
        params.put("token",token);
        return mApiService.loginOut(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<String>>() {
                               @Override
                               public void accept(Result<String> result) throws Exception {
                                   if (result.getCode()==TOKEN_ERROR){
                                       callback.onFailed("");
                                       return;
                                   }
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }


    public Disposable BindRegid(String token,String BindRegid,final ApiCallback<CodeModel> callback) {
        final Map params =new HashMap<>();
        params.put("token",token);
        params.put("registrationid",BindRegid);
        return mApiService.BindRegid(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<CodeModel>>() {
                               @Override
                               public void accept(Result<CodeModel> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }


    public Disposable ShareLink(String token,final ApiCallback<CodeModel> callback) {
        final Map params =new HashMap<>();
        params.put("token",token);
        return mApiService.ShareLink(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<CodeModel>>() {
                               @Override
                               public void accept(Result<CodeModel> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }


    public Disposable CommentVideo(String comment_type,String id,
                             String content,String token,final ApiCallback<List<CodeModel>> callback) {
        final Map params =new HashMap<>();
        params.put("comment_type",comment_type);
        params.put("id",id);
        params.put("content",content);
        params.put("token",token);
        return mApiService.CommentVideo(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<CodeModel>>>() {
                               @Override
                               public void accept(Result<List<CodeModel>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }


    public Disposable getLook(String token,final ApiCallback<List<LookHisToryModel> > callback) {
        final Map params =new HashMap<>();
        params.put("token",token);
        return mApiService.getLook(token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<LookHisToryModel> >>() {
                               @Override
                               public void accept(Result<List<LookHisToryModel> > result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }


    public Disposable getCollect( String token,final ApiCallback<List<CollectModel>> callback) {
        final Map params =new HashMap<>();
        params.put("token",token);
        return mApiService.getCollect(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<CollectModel>>>() {
                               @Override
                               public void accept(Result<List<CollectModel>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }


    public Disposable delCollect( String token,String id,String object_type,final ApiCallback<CollectModel> callback) {
        final Map params =new HashMap<>();
        params.put("token",token);
        params.put("collection_id",id);
        params.put("object_type",object_type);
        return mApiService.delCollect(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<CollectModel>>() {
                               @Override
                               public void accept(Result<CollectModel> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 获取用户信息
     * @param token
     * @param callback
     */
    public Disposable getUserInfo(String token,final ApiCallback<UserModel> callback) {

        final Map params =new HashMap<>();
        params.put("token",token);
        return mApiService.getUserInfo(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<UserModel>>() {
                               @Override
                               public void accept(Result<UserModel> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    public Disposable getFriends(String token,final ApiCallback<List<FriendModel>> callback) {

        final Map params =new HashMap<>();
        params.put("token",token);
        return mApiService.getFriends(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<FriendModel>>>() {
                               @Override
                               public void accept(Result<List<FriendModel>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 获取视频标签
     * @param callback
     */
    public Disposable getLable(final ApiCallback<List<VideoLableModel>> callback) {
        return mApiService.getLable()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<VideoLableModel>>>() {
                               @Override
                               public void accept(Result<List<VideoLableModel>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 获取视频列表
     * @param category_id
     * @param callback
     */
    public Disposable getVideoList(String category_id,final ApiCallback<List<VideoModel>> callback) {
        return mApiService.getVideoList(category_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<VideoModel>>>() {
                               @Override
                               public void accept(Result<List<VideoModel>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    public Disposable getDetail(String category_id,final ApiCallback<VideoDetailModel> callback) {
        return mApiService.getDetail(category_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<VideoDetailModel>>() {
                               @Override
                               public void accept(Result<VideoDetailModel> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    public Disposable getComment(String category_id,String page,String pageCount,final ApiCallback<VideoCommentlModel> callback) {
        return mApiService.getComment(category_id,page,pageCount)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<VideoCommentlModel>>() {
                               @Override
                               public void accept(Result<VideoCommentlModel> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    public Disposable getArticComment(String category_id,String page,String pageCount,final ApiCallback<VideoCommentlModel> callback) {
        return mApiService.getArticComment(category_id,page,pageCount)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<VideoCommentlModel>>() {
                               @Override
                               public void accept(Result<VideoCommentlModel> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    public Disposable collect( String operation, String id, String token, final ApiCallback<List<CodeModel>> callback) {
        Map map=new HashMap();
        map.put("operation",operation);
        map.put("id",id);
        map.put("token",token);
        return mApiService.collect(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<CodeModel>>>() {
                               @Override
                               public void accept(Result<List<CodeModel>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE || result.getCode()!=80002) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }


    /**
     * 圈子详情-顶部
     * @param token 圈子id
     * @param callback 回调接口
     */
    public Disposable getShareLink(String token, ApiCallback<String> callback) {
        return mApiService.getInviteUrl(token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<CodeModel>>() {
                               @Override
                               public void accept(Result<CodeModel> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData().url);
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }


    /**
     * 获取最新的版本信息
     * @param callback 回调接口
     */
    public Disposable getVersionInfo( ApiCallback<VersionInfo> callback) {
        return mApiService.getVersionInfo()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<VersionInfo>>() {
                               @Override
                               public void accept(Result<VersionInfo> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 获取最新的版本信息
     * @param callback 回调接口
     */
    public Disposable getMyTeam( String token,ApiCallback<List<TeamModel>> callback) {
        return mApiService.getMyTeam(token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<TeamModel>>>() {
                               @Override
                               public void accept(Result<List<TeamModel>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 获取最新的版本信息
     * @param callback 回调接口
     */
    public Disposable getRelation( String token,String invite_code,ApiCallback<List<TeamModel>> callback) {
        return mApiService.getRelation(token,invite_code)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<TeamModel>>>() {
                               @Override
                               public void accept(Result<List<TeamModel>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }

    /**
     * 获取最新的版本信息
     * @param callback 回调接口
     */
    public Disposable getNewsList( String type,ApiCallback<List<NewsForCategory>> callback) {
        return mApiService.getNewsList(type)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<NewsForCategory>>>() {
                               @Override
                               public void accept(Result<List<NewsForCategory>> result) throws Exception {
                                   if (result.getCode() != SUCCESS_CODE) {
                                       throw new AppException(result.getMessage());
                                   }
                                   callback.onSuccess(result.getData());
                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                String errorMsg = "系统出现未知错误，请稍后再试";
                                if (throwable instanceof AppException) {
                                    errorMsg = throwable.getMessage();
                                }
                                callback.onFailed(errorMsg);
                            }
                        });
    }


    public static final MediaType MEDIA_TYPE = MediaType.parse("application/json");
    public static RequestBody createBody(String json) {
        return RequestBody.create(MEDIA_TYPE, json);
    }
}
